package cn.bigfire.wedesk.util;

import cn.hutool.core.util.NumberUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.StringUtil;

public class ByteUtil {

    public static void log(String str) {
        try {
            log(str.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void log(byte[] bytes) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byteBuf.writeBytes(bytes);
        log(byteBuf);
    }

    public static void log(byte[] bytes, int len) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byte[] subBytes = new byte[len];
        System.arraycopy(bytes, 0, subBytes, 0, len);
        byteBuf.writeBytes(subBytes);
        log(byteBuf);
    }

    public static void log(byte[] bytes,int offset, int len) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byte[] subBytes = new byte[len];
        System.arraycopy(bytes, offset, subBytes, 0, len);
        byteBuf.writeBytes(subBytes);
        log(byteBuf);
    }

    public static void log(ByteBuf byteBuf) {
        StringBuilder dump = new StringBuilder();
        dump.append("read index:").append(byteBuf.readerIndex()).append("\t");
        dump.append("write index:").append(byteBuf.writerIndex()).append("\t");
        dump.append("capacity index:").append(byteBuf.capacity()).append(StringUtil.NEWLINE);
        ByteBufUtil.appendPrettyHexDump(dump, byteBuf);
        System.out.println(dump.toString());
    }

    public static void log(ByteBuf byteBuf, boolean in) {
        StringBuilder dump = new StringBuilder();
        dump.append("read index:").append(byteBuf.readerIndex()).append("\t");
        dump.append("write index:").append(byteBuf.writerIndex()).append("\t");
        dump.append("capacity index:").append(byteBuf.capacity()).append(StringUtil.NEWLINE);
        ByteBufUtil.appendPrettyHexDump(dump, byteBuf);
        String str = dump.toString().replace("+", in ? "<" : ">");
        System.out.println(str);
    }

    public static String size(long len) {
        int byteNum = 1024;
        if (len < byteNum) {
            return len + "B";
        } else if (len < Math.pow(byteNum, 2)) {
            return String.format("%.2fKB", len / Math.pow(byteNum, 1));
        } else if (len < Math.pow(byteNum, 3)) {
            return String.format("%.2fMB", len / Math.pow(byteNum, 2));
        } else if (len < Math.pow(byteNum, 4)) {
            return String.format("%.2fGB", len / Math.pow(byteNum, 3));
        } else {
            return String.format("%.2fT", len / Math.pow(byteNum, 4));
        }
    }


    //按顺序合并多个字节数组
    public static byte[] appends(byte[]... byteArr) {
        int sumLen = 0;
        for (byte[] bytes : byteArr) {
            sumLen += bytes.length;
        }
        byte[] combineArrays = new byte[sumLen];
        int lastPos = 0;
        for (byte[] bytes : byteArr) {
            System.arraycopy(bytes, 0, combineArrays, lastPos, bytes.length);
            lastPos += bytes.length;
        }
        return combineArrays;
    }



}


